﻿using System;
using System.ComponentModel;

namespace Coding4Fun.PanoHead.ViewModels
{
    public class PanoViewModel : INotifyPropertyChanged
    {
        private uint _pauseTime;
        private uint _settleTime;
        private uint _iterations;
        private uint _turnTime;
        private readonly uint _shutter;
        private bool _isConnected;
        private string _status;
        private string _appVersion;
        private readonly AppSettings _appSettings;

        public PanoViewModel()
        {
            _appSettings = new AppSettings();
            _shutter = 100;
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        public void LoadData()
        {
            IsDataLoaded = true;

            Iterations = _appSettings.Iterations;
            TurnTime = _appSettings.TurnTime;
            SettleTime = _appSettings.SettleTime;
            PauseTime = _appSettings.PauseTime;
        }

        public void SaveData()
        {
            _appSettings.Iterations = Iterations;
            _appSettings.PauseTime = PauseTime;
            _appSettings.SettleTime = SettleTime;
            _appSettings.TurnTime = TurnTime;
        }

        public string MakePanoCommand()
        {
            return String.Format("{0},{1},{2},{3},{4}", TurnTime, SettleTime, _shutter, PauseTime, Iterations);
        }

        public bool IsConnected
        {
            get { return _isConnected; }
            set
            {
                if (_isConnected == value)
                    return;
                _isConnected = value;
                NotifyPropertyChanged("IsConnected");
            }
        }

        public uint TurnTime
        {
            get { return _turnTime; }
            set
            {
                if (_turnTime == value)
                    return;
                _turnTime = value;
                NotifyPropertyChanged("TurnTime");
            }
        }

        public uint Iterations
        {
            get { return _iterations; }
            set
            {
                if (_iterations == value)
                    return;
                _iterations = value;
                NotifyPropertyChanged("Iterations");
            }
        }

        public uint SettleTime
        {
            get { return _settleTime; }
            set
            {
                if (_settleTime == value)
                    return;
                _settleTime = value;
                NotifyPropertyChanged("SettleTime");
            }
        }

        public uint PauseTime
        {
            get { return _pauseTime; }
            set
            {
                if (_pauseTime == value)
                    return;
                _pauseTime = value;
                NotifyPropertyChanged("PauseTime");
            }
        }

        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (value != _status)
                {
                    _status = value;
                    NotifyPropertyChanged("Status");
                }
            }
        }

        public string AppVersion
        {
            get
            {
                return _appVersion;
            }
            set
            {
                if (value != _appVersion)
                {
                    _appVersion = value;
                    NotifyPropertyChanged("AppVersion");
                }
            }
        }

        public AppSettings AppSettings
        {
            get { return _appSettings; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
